#include <glib.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlreader.h>
#include "libtiffanygirl.h"
#include "tiffanygirl_note.h"

TGirlNote *tiffanygirl_note_new(const char *id, const char *title)
{
	TGirlNote *tgnote;

	tgnote = (TGirlNote *)g_new0(TGirlNote, 1);
	tgnote->id = id;
	tgnote->title = title;

	return tgnote;
}

gchar *tiffanygirl_note_read_title(const char *filename)
{
	xmlDocPtr doc;
	xmlNodePtr curnode;
	xmlChar *xmlTitle;
	gchar *title = NULL;

	doc = xmlParseFile(filename);
	if (doc == NULL) {
		printf("Note Document not parsed successfully.\n");
		return NULL;
	}

	curnode = xmlDocGetRootElement(doc);
	if (curnode == NULL) {
		printf("Note Document is empty.\n");
		xmlFreeDoc(doc);
		return NULL;
	}

	if (xmlStrcmp(curnode->name, (const xmlChar *) "note")) {
		printf("Incorrect document type, root node should have been note\n");
		xmlFreeDoc(doc);
		return NULL;
	}

	curnode = curnode->xmlChildrenNode;
	while (curnode!=NULL) {
		if (!xmlStrcmp(curnode->name, (const xmlChar *) "title")) {
			//printf("%s\n",  xmlNodeGetContent(curnode));
			xmlTitle = xmlNodeGetContent(curnode);
			title = g_strdup((gchar *)xmlTitle);
			xmlFree(xmlTitle);
			break;
		}
		curnode = curnode->next;
	}

	xmlFreeDoc(doc);
	xmlCleanupParser();

	return title;
}

gchar *tiffanygirl_note_read_text(const char *id)
{
	xmlTextReaderPtr reader;
	gchar filename[256];
	gchar *name, *text = NULL;
	gint ret;

	/* open XML file */
	sprintf(filename, "%s/.tomboy/%s.note", g_get_home_dir(), id);
	reader = xmlReaderForFile(filename, NULL, 0);
	if (reader==NULL) {
		printf("Note Document not parsed successfully.\n");
		return NULL;
	}

	while(xmlTextReaderRead(reader)) {
		name = (gchar *)xmlTextReaderConstName(reader);
		if (name==NULL)
			continue;

		if (strcmp(name, "text")==0) {
			text = (gchar *)xmlTextReaderReadInnerXml(reader);
			break;
		}
	}

	xmlFreeTextReader(reader);
	xmlCleanupParser();
	xmlMemoryDump();

	return text;
}

TGirlNoteData *tiffanygirl_note_read(const char *id)
{
	TGirlNoteData *tgnotedata;
	xmlTextReaderPtr reader;
	gchar filename[256];
	gchar *name, *text = NULL;
	gint ret;

	/* open XML file */
	sprintf(filename, "%s/.tomboy/%s.note", g_get_home_dir(), id);
	reader = xmlReaderForFile(filename, NULL, 0);
	if (reader==NULL) {
		printf("Note Document not parsed successfully.\n");
		return NULL;
	}

	/* allocate */
	tgnotedata = (TGirlNoteData *)g_new0(TGirlNoteData, 1);
	tgnotedata->id = g_strdup(id);
	tgnotedata->text = NULL;

	while(xmlTextReaderRead(reader)) {
		name = (gchar *)xmlTextReaderConstName(reader);
		if (name==NULL)
			continue;

		if (strcmp(name, "title")==0) {
			tgnotedata->title = (gchar *)xmlTextReaderReadInnerXml(reader);
		} else if (strcmp(name, "text")==0) {
			if (tgnotedata->text==NULL) {
				tgnotedata->text = (gchar *)xmlTextReaderReadInnerXml(reader);
			}
			continue;
		} else if (strcmp(name, "width")==0) {
			text = (gchar *)xmlTextReaderReadInnerXml(reader);
			tgnotedata->width = atoi(text);
			g_free(text);
		} else if (strcmp(name, "height")==0) {
			text = (gchar *)xmlTextReaderReadInnerXml(reader);
			tgnotedata->height = atoi(text);
			g_free(text);
		} else if (strcmp(name, "x")==0) {
			text = (gchar *)xmlTextReaderReadInnerXml(reader);
			tgnotedata->x = atoi(text);
			g_free(text);
		} else if (strcmp(name, "y")==0) {
			text = (gchar *)xmlTextReaderReadInnerXml(reader);
			tgnotedata->y = atoi(text);
			g_free(text);
		} else if (strcmp(name, "cursor-position")==0) {
			text = (gchar *)xmlTextReaderReadInnerXml(reader);
			tgnotedata->cursor = atoi(text);
			g_free(text);
		} else
			continue;
 
		xmlTextReaderRead(reader);
		xmlTextReaderRead(reader);
	}
/*
	printf("%s\n", tgnotedata->text);
	printf("%d\n", tgnotedata->width);
	printf("%d\n", tgnotedata->height);
	printf("%d\n", tgnotedata->x);
	printf("%d\n", tgnotedata->y);
	printf("%d\n", tgnotedata->cursor);
*/
	xmlFreeTextReader(reader);
	xmlCleanupParser();
	xmlMemoryDump();

	return tgnotedata;
}
